function [Event ok] = psg_DetectSpindle(handles)
%----------------------------------------------------------
% Detect Sleep Spindle
%
% Head
% Data
%
% Author : Gwan-Taek Lee
% Last update : 2011. 12. 20
%----------------------------------------------------------

if (handles.n_file > 0) && strcmpi(handles.mode, 'file')
  
    parms = inputdlg({'Select Channels',...
                      'Sleep Stage',...
                      'Frequency Range (Hz) - Min Max',...
                      'Potential PDF alpha',...
                      'Energy Ratio (0~1)',...
                      'Min Spindle Duration (ms)',...
                      'Matching Window',...
                      'Correlation alpha'},'',1,...
                     {'F3 F4 C3 C4 O1 O2','N2','10 16','0.01','0.5','400','triang','0.01'});
                 
    if ~isempty(parms)

        ChanLab  = gui_GetParms(parms{1}, '%s', ' ');        
        StageLab = gui_GetParms(parms{2}, '%s', ' ');
        fband    = gui_GetParms(parms{3}, '%f %f', ' ');
        palpha   = gui_GetParms(parms{4}, '%f', ' ');
        ratio    = gui_GetParms(parms{5}, '%f', ' ');
        mindur   = gui_GetParms(parms{6}, '%f', ' ');
        matwin   = parms{7};
        calpha   = gui_GetParms(parms{8}, '%f', ' ');

        for i = 1 : length(handles.i_file)
            disp(['Processing: ' handles.Head(handles.i_file(i)).FileName]);
            pause(0.000000000001);
            Head  = handles.Head(handles.i_file(i));
            name  = Head.FileName;
            path  = Head.FilePath;
            fs    = Head.SampRate;
            stime = Head.StartDate + Head.StartTime;
           
            chan  = util_GetIndex(Head.ChanLabel, ChanLab);
            stage = util_GetIndex(Head.Stage.Label, StageLab);
            fwin  = 'hann';
            fmul   = '5';

            Data  = file_Load(name,path,'Data');
            Data  = util_DataReference(Data, Head.RefeChan);
            
            Event = [];
            for c = 1 : length(chan)
                X                     = FirFilter(Data(chan(c),:),fs,fband,fwin,fmul);
                X                     = abs(hilbert(X));
                candidate             = psg_SpindleCriteria1(Head, X, stage, palpha);
                [candidate candisamp] = psg_SpindleCriteria2(Head, X, candidate, ratio, mindur);
                candidate             = psg_SpindleCriteria3(candidate, candisamp, matwin, calpha);
                
                % Remove overlap
                s = 1;
                while s < length(candidate)
                    if (candidate(s+1) - candidate(s)) < (2*mindur/(1000/fs))
                        candidate(s+1) = [];
                    else
                        s = s + 1;
                    end
                end
                
                event.Label   = 'Spindle';
                event.Time    = util_GetOffTime(stime, fs, candidate);
                event.Channel = Head.ChanLabel(chan(c));
                Event         = [Event event];
            end
            
            Head.Event = [Head.Event Event];
            file_Save(Head,[],[],[]);
            handles.Head(handles.i_file(i)) = Head;
        end
        
        ok = 1;
        
    else
        ok = 0;
    end
    
else    
    msgbox('Select Files!!!','Error','error');
    ok = 0;   
end
